FROM alpine:latest
MAINTAINER blowhunter  <blowhunter@foxmail.com>

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

RUN apk add --update-cache --virtual build-deps --no-cache \
  autoconf \
  automake \
  cmake \
  libtool \
  libuv \
  linux-headers \
  m4 \
  unzip \
  clang \
  xz \
  make \
  openssl

RUN apk add --no-cache ncurses ncurses-dev ncurses-libs ncurses-terminfo \
  g++ \
  git \
  python3 \
  python3-dev \
  py3-pip \
  ctags \
  libintl \
  curl \
  libcurl \
  gettext-dev \
  fzf \
  bash

# make soft link to the path of python3
RUN ln -s /usr/bin/python3 /usr/bin/python

# copy the personal configuration files and install neovim shell scripts
RUN mkdir /tmp/autoload /tmp/neovim
COPY init.toml /tmp/init.toml
COPY autoload/myspacevim.vim /tmp/autoload/myspacevim.vim

RUN export https_proxy=socks5://192.168.3.12:1080
RUN export http_proxy=socks5://192.168.3.12:1080

# # Required for running this on my archbox
RUN git config --global http.https://github.com.proxy socks5://192.168.3.12:1080
RUN git config --global https.https://github.com.proxy socks5://192.168.3.12:1080

# starting install neovim
# first dependency
WORKDIR /tmp
RUN git clone --depth=1 https://github.com/neovim/libtermkey.git && \
  cd libtermkey && \
  make && \
  make install && \
  cd ../ && rm -rf libtermkey
RUN git clone --depth=1 https://github.com/neovim/libvterm.git && \
  cd libvterm && \
  make && \
  make install && \
  cd ../ && rm -rf libvterm
RUN git clone https://github.com/neovim/unibilium.git && \
  cd unibilium && \
  make && \
  make install && \
  cd ../ && rm -rf unibilium

# last neovim
RUN git clone https://github.com/neovim/neovim.git && \
  cd neovim && \
  make CMAKE_BUILD_TYPE=RelWithDebInfo                  \
       CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=/usr/local" && \
  make install && \
  cd ../ && rm -rf neovim

# starting install spacevim ...
# clear and create user  and group
ENV HOME /home/spacevim
ENV USER=spacevim
# for special userid and groupid
# ENV UID=5515
# ENV GID=5515
RUN deluser guest \
  && delgroup users \
  && addgroup spacevim \
  && adduser --disabled-password \
             -h $HOME \
             -G spacevim \
             spacevim
USER spacevim

WORKDIR $HOME

ENV PATH "$HOME/.local/bin:${PATH}"
# make directory needed
RUN mkdir -p $HOME/.config $HOME/.SpaceVim.d/autoload


# Install neovim python support
RUN pip install --user neovim yapf flake8 isort jedi pylint

RUN curl -sLf https://spacevim.org/cn/install.sh | bash

# copy the personal configuration files
COPY init.toml $HOME/.SpaceVim.d/init.toml
COPY autoload/myspacevim.vim $HOME/.SpaceVim.d/autoload/myspacevim.vim

# Get plugin and install
RUN nvim --headless +'call dein#install()' +qall
RUN nvim --headless +"call dein#add('Shougo/vimproc.vim', {'build' : 'make'})" +qall
RUN nvim -c ':UpdateRemotePlugins' -c ':qall'


USER root
# remove temporary dirs and files
RUN apk del build-deps
RUN rm -rf /var/cache/apk/* \
        rm -rf /tmp/*

USER spacevim
WORKDIR $HOME

ENV TERM xterm256-color

ENTRYPOINT /bin/ash
# ENTRYPOINT /usr/local/bin/nvim
